package com.yiwei.ydd.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.ChatCenterActivity;
import com.yiwei.ydd.activity.MyOrderDetailActivity;
import com.yiwei.ydd.api.model.MyOrderListModel;
import com.yiwei.ydd.util.Arith;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyOrderAdapter extends ItemAdapter<MyOrderListModel.DatasBean, MyOrderAdapter.DefaultHolder> {
    private final int textSize28;
    private final int textSize24;
    private final int textSize32;
    private final int textSize22;

    private Context context;

    public MyOrderAdapter(Context context) {
        super(context);
        textSize28 = context.getResources().getDimensionPixelSize(R.dimen.x28);
        textSize24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
        textSize32 = context.getResources().getDimensionPixelSize(R.dimen.x32);
        textSize22 = context.getResources().getDimensionPixelSize(R.dimen.x22);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_order, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        MyOrderListModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.txtTitle.setText(item.name);
        if (TextUtils.isEmpty(item.card_number) && (item.status.equals("2")||item.status.equals("3"))) {
            holder.txtCardType.setText("请在48小时内添加加油卡");
            holder.imgCardType.setImageResource(R.mipmap.img_oil_icon_order);
        } else {
            if (item.status.equals("0")) {
                holder.txtCardType.setText("请在24小时内付款");
            } else {
                String strsub = "";
                if (item.card_number.length() >= 4) {// 判断是否长度大于等于4
                    strsub = item.card_number.substring(item.card_number.length() - 4);//一个参数表示截取传递的序号之后的部分
                } else {
                    strsub = item.card_number;
                }
                Spanned cardType = Spans.with(context).font(item.card_type, textSize28, 0xff333333).
                        font("(卡尾号" + strsub + ")", textSize24, 0xffFB5E01).
                        build();
                holder.txtCardType.setText(cardType);
            }
            if(item.card_type.contains("石化")){
                holder.imgCardType.setImageResource(R.mipmap.img_order_shihua);
            }else if(item.card_type.contains("石油")){
                holder.imgCardType.setImageResource(R.mipmap.img_order_shiyou);
            }else {
                holder.imgCardType.setImageResource(R.mipmap.img_oil_icon_order);
            }

        }


//  （-2：失效，-1：失败，0：待支付，1：完成，2：已付款，3：分期中）
        switch (item.status) {
            case "-2":
                if (item.type.equals("1")) {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("重新购买");
                    holder.btnTwo.setVisibility(View.VISIBLE);
                    holder.btnTwo.setText("删除订单");
                } else {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("重新购买");
                    holder.btnTwo.setVisibility(View.VISIBLE);
                    holder.btnTwo.setText("删除订单");
                }
                holder.txtStatus.setText("失效");
                holder.txtCardType.setText("该订单已失效，请重新购买");
                break;
            case "-1":
                if (item.type.equals("1")) {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("重新购买");
                    holder.btnTwo.setVisibility(View.GONE);
                } else {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("重新购买");
                    holder.btnTwo.setVisibility(View.GONE);
                }
                holder.txtStatus.setText("失败");
                break;
            case "0":
                holder.btnBuy.setVisibility(View.VISIBLE);
                holder.btnBuy.setText("去付款");
                holder.btnTwo.setVisibility(View.GONE);
                holder.txtStatus.setText("待付款");
                break;
            case "1":
                holder.btnBuy.setVisibility(View.VISIBLE);
                holder.btnBuy.setText("再次购买");
                holder.btnTwo.setVisibility(View.GONE);
                holder.txtStatus.setText("已完成");
                break;
            case "2":
                if (TextUtils.isEmpty(item.card_number)) {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("选择油卡");
                    holder.txtStatus.setText("待加油卡");
                } else {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("再次购买");
                    holder.txtStatus.setText("已付款");
                }

                holder.btnTwo.setVisibility(View.GONE);

                break;
            case "3":
//                if (item.type.equals("1")) {
//                    holder.btnBuy.setVisibility(View.GONE);
//                } else {
                if (TextUtils.isEmpty(item.card_number)) {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("选择油卡");
                    holder.txtStatus.setText("待加油卡");
                } else {
                    holder.btnBuy.setVisibility(View.VISIBLE);
                    holder.btnBuy.setText("返现记录");
                    holder.txtStatus.setText("加油中");
                }
//                }

                holder.btnTwo.setVisibility(View.VISIBLE);
                holder.btnTwo.setText("联系客服");

                break;
        }

//（1：快速充值，2：分期充值）
        if (item.type.equals("1")) {
            Spanned money = Spans.with(context).font("￥" + item.pay_money, textSize32, 0xffFB5E01).style(Typeface.BOLD).
                    font(" " + Arith.mul(Float.valueOf(item.sale), 0.1) + "折", textSize22, 0xff999999).
                    build();
            holder.txtContent.setText(money);
        } else if (item.type.equals("2")) {
            Spanned money = Spans.with(context).font("￥" + item.pay_money, textSize32, 0xffFB5E01).style(Typeface.BOLD).
                    font(" 多赠送" + item.git + "%(即" + Arith.mul(Arith.mul(Double.valueOf(item.pay_money), Double.valueOf(item.git)), 0.01) + "元)", textSize22, 0xff999999).
                    build();
            holder.txtContent.setText(money);
        }

//        if (item.status.equals("0")) {
//            holder.btnBuy.setVisibility(View.VISIBLE);
//        } else {
//            holder.btnBuy.setVisibility(View.GONE);
//        }
//        if (item.card_type.equals("中国石油")) {
//            holder.imgCardType.setImageResource(R.mipmap.img_type_shiyou);
//        } else if (item.card_type.equals("中国石化")) {
//            holder.imgCardType.setImageResource(R.mipmap.img_type_shihua);
//        }
    }


    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_two)
        SuperButton btnTwo;
        @BindView(R.id.txt_card_type)
        TextView txtCardType;
        @BindView(R.id.txt_status)
        TextView txtStatus;
        @BindView(R.id.img_card_type)
        ImageView imgCardType;
        @BindView(R.id.fl_card_type)
        FrameLayout flCardType;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.btn_buy)
        SuperButton btnBuy;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        MyOrderListModel.DatasBean item;

        @OnClick({R.id.btn_buy, R.id.layout_total, R.id.btn_two})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_two:
                    if (UI.toString(btnTwo).equals("删除订单")) {
                        if (onChooseListener != null) {
                            onChooseListener.onDelete(item.order_no);
                        }
                    } else {
                        Util.startActivity(context, ChatCenterActivity.class);
                    }

                    break;
                case R.id.btn_buy:
                    if (UI.toString(btnBuy).equals("返现记录")) {
                        if (onChooseListener != null) {
                            onChooseListener.onFanXian();
                        }
                    } else if (UI.toString(btnBuy).equals("去付款")) {
                        if (onChooseListener != null) {
                            onChooseListener.onPayOrder(item.pay_money, item.order_no);
                        }
                    } else if (UI.toString(btnBuy).equals("重新购买")) {
                        if (item.type.equals("1")) {
                            if (onChooseListener != null) {
                                onChooseListener.onBuyNow();
                            }
                        } else {
                            if (onChooseListener != null) {
                                onChooseListener.onBuyPacket();
                            }
                        }
                    } else if (UI.toString(btnBuy).equals("再次购买")) {
                        if (item.type.equals("1")) {
                            if (onChooseListener != null) {
                                onChooseListener.onBuyNow();
                            }
                        } else {
                            if (onChooseListener != null) {
                                onChooseListener.onBuyPacket();
                            }
                        }
                    } else if (UI.toString(btnBuy).equals("重新购买")) {
                        if (onChooseListener != null) {
                            onChooseListener.onBuyPacket();
                        }
                    } else if (UI.toString(btnBuy).equals("选择油卡")) {
                        if (onChooseListener != null) {
                            onChooseListener.onAddCard(item.order_no);
                        }
                    }

                    break;
                case R.id.layout_total:
                    Intent intent = new Intent(context, MyOrderDetailActivity.class);
                    intent.putExtra("order_number", item.order_no);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    break;
            }
        }

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onPayOrder(String money, String order_no);

        void onFanXian();

        void onBuyNow();

        void onBuyPacket();

        void onDelete(String order_no);

        void onAddCard(String order_no);
    }
}
